package com.cxx.home.service.impl;

import com.cxx.framework.web.CustomException;
import com.cxx.home.dao.JournalDao;
import com.cxx.home.dto.journal.JournalDto;
import com.cxx.home.entity.Journal;
import com.cxx.home.mapper.JournalMapper;
import com.cxx.home.service.JournalService;
import com.cxx.home.vo.JournalQueryVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JournalServiceImpl implements JournalService {
    @Resource
    private JournalDao journalDao;

    @Resource
    private JournalMapper journalMapper;

    @Override
    public Boolean addJournal(JournalDto journalDto) {
        Journal journal = new Journal();
        BeanUtils.copyProperties(journalDto, journal);

        return journalMapper.insert(journal) == 1;
    }

    @Override
    public Boolean updateJournal(Long id, JournalDto journalDto) {
        if (journalMapper.selectById(id) == null) {
            throw new CustomException("该日记不存在");
        }

        Journal journal = new Journal();
        BeanUtils.copyProperties(journalDto, journal);
        journal.setId(id);
        return journalMapper.updateById(journal) == 1;
    }

    @Override
    public Boolean deleteJournal(Long id) {
        if (journalMapper.selectById(id) == null) {
            throw new CustomException("该纪念日不存在");
        }

        return journalMapper.deleteById(id) == 1;
    }

    @Override
    public List<JournalDto> queryJournal(JournalQueryVo queryVo) {
        return journalDao.queryJournal(queryVo);
    }
}
